package com.djakapm.markdown.editor.component;

import com.djakapm.markdown.editor.AppConstant;
import com.djakapm.markdown.editor.component.highlighter.MarkdownEditorKit;
import com.djakapm.markdown.editor.component.listener.MarkdownTextEditorContextMenuListener;
import com.djakapm.markdown.editor.component.listener.TextChangeKeyListener;
import com.djakapm.markdown.editor.component.listener.UndoListener;
import com.djakapm.swing.component.EditorPaneView;
import com.djakapm.swing.container.BorderPanel;

import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.undo.UndoManager;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class MarkdownTextEditor extends BorderPanel {
    private EditorPaneView textEditor;
    private JEditorPane textComponent;
    private static ActionMap sharedActionMap; 
    private JFileChooser fileDialog;
    {
        this.fileDialog = new JFileChooser();
        this.fileDialog.setFileFilter(new AppFileFilter());
        this.fileDialog.setAcceptAllFileFilterUsed(false);
    }
    private JToolBar toolbar = new JToolBar();
    private JPopupMenu popupMenu = new JPopupMenu();


    public MarkdownTextEditor(){
        this.textEditor = new EditorPaneView();
        this.textComponent = this.textEditor.getComponent();
        this.addToNorth(getToolBar());
        this.addToCenter(this.textEditor);
    }

    public JToolBar getToolBar() {
        return this.toolbar;
    }

    public JFileChooser getFileDialog(){
        return this.fileDialog;
    }


    public void configureEditor(UndoManager undoManager,TextChangeKeyListener keyListener,
                                MarkdownTextEditorContextMenuListener markdownTextEditorContextMenuListener
                                ,Action... actions){

        Document doc = this.textComponent.getDocument();
        doc.addUndoableEditListener(new UndoListener(undoManager));

        this.textComponent.addMouseListener(markdownTextEditorContextMenuListener);
        this.textComponent = this.textEditor.getComponent();
        this.textComponent.setEditorKitForContentType(AppConstant.MARKDOWN_CONTENT_TYPE, new MarkdownEditorKit());
        this.textComponent.setContentType(AppConstant.MARKDOWN_CONTENT_TYPE);

        this.textComponent.addKeyListener(keyListener);
        this.textComponent.getActionMap().put(AppConstant.SAVE_FILE_ACTION,actions[0]);
        this.textComponent.getActionMap().put(AppConstant.UPDATE_VIEW_ACTION,actions[1]);


        this.textComponent.getInputMap().put(KeyStroke.getKeyStroke('O', InputEvent.CTRL_MASK),AppConstant.OPEN_FILE_ACTION);
        this.textComponent.getInputMap().put(KeyStroke.getKeyStroke('S', InputEvent.CTRL_MASK),AppConstant.SAVE_FILE_ACTION);
        this.textComponent.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_MASK),AppConstant.UPDATE_VIEW_ACTION);

        this.textComponent.getActionMap().put(AppConstant.UNDO_ACTION,actions[2]);
        this.textComponent.getActionMap().put(AppConstant.REDO_ACTION,actions[3]);

        this.textComponent.getInputMap().put(KeyStroke.getKeyStroke('Z',InputEvent.CTRL_MASK),AppConstant.UNDO_ACTION);
        this.textComponent.getInputMap().put(KeyStroke.getKeyStroke('Z',InputEvent.CTRL_MASK + InputEvent.SHIFT_MASK),AppConstant.REDO_ACTION);
    }

    public void setText(String content){
        this.textEditor.setText(content);
    }


    public String getText(){
        return this.textEditor.getText();
    }


    public JPopupMenu getPopupMenu() {
        return popupMenu;
    }

    public ActionMap getEditorActionMap(){
        if(sharedActionMap == null){
            sharedActionMap = this.textComponent.getActionMap();
        }
        return sharedActionMap;
    }
}